﻿using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace P7_todoList_wpf;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private TodoItemManager _manager = new TodoItemManager();
    private List<TodoItem>? _todos;
    public ObservableCollection<TodoItem> TodoItems { get; set; } = new ObservableCollection<TodoItem>();

    public MainWindow()
    {
        InitializeComponent();
        TodoItems = _manager.GetTodos;
        TodoListBox.ItemsSource = TodoItems;
    }

    private void AddTodoItem(object sender, RoutedEventArgs e)
    {

        if (string.IsNullOrWhiteSpace(Title.Text))
        {
            MessageBox.Show("请输入标题！");
            return;
        }
      
        TodoItem newTodoItem = new TodoItem();
        newTodoItem.Title = Title.Text;
        newTodoItem.Description = Description.Text;
        newTodoItem.IsCompleted  = false;
        newTodoItem.Category = Category.Text;
        newTodoItem.DueDate = DueDate.DisplayDate;
        _manager.AddTodoItem(newTodoItem);
        Title.Clear();
        Description.Clear();
        Category.TabIndex = -1;
    }

    private void CompleteButton_Click(object sender, RoutedEventArgs e)
    {
        if (sender is Button btn && btn.DataContext is TodoItem item)
        {
            item.IsCompleted = !item.IsCompleted;
            // 强制刷新界面（最简单的方法）
            TodoListBox.Items.Refresh();
        }
    }

    private void DeleteButton_Click(object sender, RoutedEventArgs e)
    {
        if (sender is Button btn && btn.DataContext is TodoItem item)
        {
            TodoItems.Remove(item);
        }
    }
}